package com.vaadin.demo.workoutlog;

import com.vaadin.demo.workoutlog.pojo.Type;
import com.vaadin.demo.workoutlog.pojo.Workout;
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.access.DataContext;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: Dmitry Zhuravlev
 * Date: 27/09/11
 * Time: 17:47
 * To change this template use File | Settings | File Templates.
 */
public class CayenneUtil {

  public static void insertExampleData(int trainingsToLoad) {
    ObjectContext context = DataContext.createDataContext();

    Type type = context.newObject(Type.class);
    type.setDate(new Date());
    type.setTitle("Running");
    type.setKilometers(123f);

    Type type2 = context.newObject(Type.class);
    type2.setDate(new Date());
    type2.setTitle("Swimming");
    type2.setKilometers(123f);
    context.commitChanges();

    // insert some sample data
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MILLISECOND, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MINUTE, 0);

    String[] titles = new String[]{"A short easy one", "intervals",
      "very long", "just shaking legs after work",
      "long one with Paul", "test run"};

    c.add(Calendar.DATE, -trainingsToLoad);

    Random rnd = new Random();

    Workout r;

    for (int i = 0; i < trainingsToLoad; i++) {
      r = new Workout();
      c.set(Calendar.HOUR_OF_DAY,
            12 + (rnd.nextInt(11) - rnd.nextInt(11)));
      r.setDate(c.getTime());
      r.setTitle(titles[rnd.nextInt(titles.length)]);
      r.setKilometers(new Float(Math.round(rnd.nextFloat() * 30)));
      r.setTrainingType(type);
      context.commitChanges();
      c.add(Calendar.DATE, 1);
    }
  }
}
